#ifndef OPTIONS_HPP
#define OPTIONS_HPP

#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>

namespace po = boost::program_options;

class Options {
public:
    int threads = 1;
    std::vector<std::string> input_files;
    std::string output_file;
    std::string out_type = "json";
    bool parse_success = false;
public:
    Options(int argc, char** argv) {
        try{
            po::options_description desc("Allowed options");
            desc.add_options()
                ("help,h", "produce help message")
                ("threads,t", po::value<int>(&threads)->default_value(1), "number of threads to use")
                ("input-files,i", po::value<std::vector<std::string>>(), "input files")
                ("output-file,o", po::value<std::string>(), "output file")
                ("output-type,T", po::value<std::string>(&out_type)->default_value("json"), "output type (json or csv)")
                ;

            po::positional_options_description p;
            p.add("input-files", -1);

            po::variables_map vm;
            po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
            po::notify(vm);

            if (vm.count("help")) {
                std::cout << desc << std::endl;
                exit(0);
            }

            if (vm.count("input-files")) {
                std::vector<std::string> input_files_tmp = vm["input-files"].as<std::vector<std::string>>();
                for (auto& file : input_files_tmp) {
                    if (!boost::filesystem::exists(file)) {
                        std::cerr << "Error: input file " << file << " does not exist" << std::endl;
                    }else{
                        input_files.push_back(file);
                    }
                }
            }

            if (vm.count("output-file")) {
                output_file = vm["output-file"].as<std::string>();
            }

            if (input_files.empty()) {
                std::cerr << "Error: no input files specified" << std::endl;
                exit(1);
            }

            if (output_file.empty()) {
                std::cerr << "Error: no output file specified" << std::endl;
                exit(1);
            }

            if (out_type!= "json" && out_type != "csv") {
                std::cerr << "Error: output type must be json or csv" << std::endl;
                exit(1);
            }

            parse_success = true;
        } catch (const std::exception& e) {
            std::cerr << "Error: " << e.what() << std::endl;
            exit(1);
        }
    }
};
#endif // OPTIONS_HPP